/*
 * Copyright (c) 2006-2021, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2021-12-1       zkw        first version
 */

#include <rtthread.h>
#include "os_mq_proc.h"
#include "app_websocket_mgnt_mp.h"
#include "app_websocket_mgnt_sm.h"

#define LOG_TAG "websocket.mp"
#define LOG_LVL LOG_LVL_INFO
#include "ulog.h"

extern os_mq_t app_webclient_tx_msg_handle;
extern os_mq_t app_webclient_rx_msg_handle;

static void app_webclient_mp_ocpp_msg(os_mq_data_pt ptMsg);
static void app_webclient_mp_15118_msg(os_mq_data_pt ptMsg);
static void app_webcleint_mp_recv_data(os_mq_data_pt ptMsg);
static void app_webclient_rx_mp_recv_data(os_mq_data_pt ptMsg);

static const os_mq_proc_t app_webclient_tx_msg_proc_tab[] =
    {
        {APP_WEBCLIENT_TX_QMT_START, 			NULL},
        {APP_WEBCLIENT_TX_QMT_OCPP_MGNT, 		app_webclient_mp_ocpp_msg},
        {APP_WEBCLIENT_TX_QMT_15118_MGNT, 		app_webclient_mp_15118_msg},
        {APP_WEBCLIENT_TX_QMT_RESP_DATA, 		app_webcleint_mp_recv_data},
        {APP_WEBCLIENT_TX_QMT_MAX, 				NULL},

};

static const os_mq_proc_t app_webclient_rx_msg_proc_tab[] =
    {
        {APP_WEBCLIENT_RX_QMT_START, 			NULL},
        {APP_WEBCLIENT_RX_QMT_RECV_DATA, 		app_webclient_rx_mp_recv_data},
        {APP_WEBCLIENT_RX_QMT_MAX, 				NULL},
};

static void app_webclient_mp_ocpp_msg(os_mq_data_pt ptMsg) {
    app_webclient_tx_sm_evt_proc(E_APP_WEBCLINET_EVT_OCPP_MSG, ptMsg);
}

static void app_webclient_mp_15118_msg(os_mq_data_pt ptMsg) {
    app_webclient_tx_sm_evt_proc(E_APP_WEBCLINET_EVT_15118_MSG, ptMsg);
}

static void app_webcleint_mp_recv_data(os_mq_data_pt ptMsg) {
    app_webclient_tx_sm_evt_proc(E_APP_WEBCLINET_EVT_DATA_RECV, ptMsg);
}

static void app_webclient_rx_mp_recv_data(os_mq_data_pt ptMsg) {
    app_webclient_rx_sm_evt_proc(E_APP_WEBCLINET_RX_EVT_RECV, ptMsg);
}

/************************************************************************


************************************************************************/
BOOL app_webclient_tx_msg_tab_init(void) {
    return os_mq_proc_array_init(APP_WEBCLIENT_TX_QMT_START, APP_WEBCLIENT_TX_QMT_MAX,
                                 ARRAY_SIZE(app_webclient_tx_msg_proc_tab),
                                 app_webclient_tx_msg_proc_tab);
}

BOOL app_webclient_rx_msg_tab_init(void) {
    return os_mq_proc_array_init(APP_WEBCLIENT_RX_QMT_START, APP_WEBCLIENT_RX_QMT_MAX,
                                 ARRAY_SIZE(app_webclient_rx_msg_proc_tab),
                                 app_webclient_rx_msg_proc_tab);
}

/************************************************************************
????

************************************************************************/
BOOL app_webclient_tx_msg_proc(os_mq_data_pt msg) {
    return os_mq_proc_func(APP_WEBCLIENT_TX_QMT_START, APP_WEBCLIENT_TX_QMT_MAX, app_webclient_tx_msg_proc_tab, msg);
}

BOOL app_webclient_rx_msg_proc(os_mq_data_pt msg) {
    return os_mq_proc_func(APP_WEBCLIENT_RX_QMT_START, APP_WEBCLIENT_RX_QMT_MAX, app_webclient_rx_msg_proc_tab, msg);
}

/************************************************************************

************************************************************************/
// get ocpp mgnt msg
BOOL app_webclient_ocpp_msg_post(struct app_websocket_msg_data_t *pt_msg_data) {
    os_mq_data msg = {0};

    if (os_mq_get_mem(&msg, APP_WEBCLIENT_TX_QMT_OCPP_MGNT, sizeof(struct app_websocket_msg_data_t))) {
        memcpy(msg.mq_pt_data, pt_msg_data, sizeof(struct app_websocket_msg_data_t));
        return os_mq_send(app_webclient_tx_msg_handle, &msg, FALSE);
    }
    return false;
}

// get websocket response msg
BOOL app_webclient_resp_data_msg_post(struct app_websocket_msg_data_t *pt_msg_data) {
    os_mq_data msg = {0};
    if (os_mq_get_mem(&msg, APP_WEBCLIENT_TX_QMT_RESP_DATA, sizeof(struct app_websocket_msg_data_t))) {
        memcpy(msg.mq_pt_data, pt_msg_data, sizeof(struct app_websocket_msg_data_t));
        return os_mq_send(app_webclient_tx_msg_handle, &msg, FALSE);
    } else {
        LOG_W("get msg err");
        return FALSE;
    }
    return TRUE;
}

//
BOOL app_webclient_recv_data_msg_post(app_websocket_recv_msg_data_t *pt_msg_data) {
    os_mq_data msg = {0};

    if (os_mq_get_mem(&msg, APP_WEBCLIENT_RX_QMT_RECV_DATA, sizeof(app_websocket_recv_msg_data_t))) {
        memcpy(msg.mq_pt_data, pt_msg_data, sizeof(app_websocket_recv_msg_data_t));
        return os_mq_send(app_webclient_rx_msg_handle, &msg, FALSE);
    }
    return false;
}

BOOL app_ios15118_ocpp_msg_post(struct app_websocket_msg_data_t *pt_msg_data) {
    os_mq_data msg = {0};

    if (os_mq_get_mem(&msg, APP_WEBCLIENT_TX_QMT_15118_MGNT, sizeof(struct app_websocket_msg_data_t))) {
        memcpy(msg.mq_pt_data, pt_msg_data, sizeof(struct app_websocket_msg_data_t));
        return os_mq_send(app_webclient_tx_msg_handle, &msg, FALSE);
    }
    return false;
}
